package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String userId) {
        // 检查用户是否存在，不存在则创建
        if (!exists(userId)) {
            User newUser = new User();
            newUser.setUserId(userId);
            userMapper.insert(newUser);
        }
        return userMapper.findById(userId);
    }

    @Override
    public User getUserInfo(String userId) {
        return userMapper.findById(userId);
    }

    @Override
    public boolean updateUserInfo(User user) {
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean exists(String userId) {
        return userMapper.findById(userId) != null;
    }
}